<template>
  <div class="app-container">
    <PageSearch :showSearch.sync="showSearch" :getList="getList" :handleQuery="getList" :resetQuery="resetQuery">
      <template #pagination>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </template>
      <template #form>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
          <el-row>
            <el-form-item prop="type">
              <el-select @change="getList" clearable :placeholder="$t('类型')" v-model="queryParams.type">
                <el-option
                  v-for="item in sku_shop_record_type_list"
                  :key="item.dictValue"
                  :value="item.dictValue"
                  :label="item.dictLabel"
                />
              </el-select>
            </el-form-item>
            <el-form-item prop="shopSku">
              <el-input v-model="queryParams.shopSku" :placeholder="$t('店铺sku')" />
            </el-form-item>
          </el-row>
        </el-form>
      </template>
      <template #actions> </template>
    </PageSearch>
    <el-table
      :height="calTableHeight()"
      ref="TableRef"
      :data="tableData"
      @selection-change="(rows) => (tableCheck = rows)"
    >
      <el-table-column show-overflow-tooltip align="center" :label="$t('店铺sku')" prop="shopSku" />
      <el-table-column align="center" show-overflow-tooltip prop="imgUrl" :label="$t('图片')">
        <template slot-scope="{ row }">
          <el-image
            style="width: 40px; height: 40px; object-fit: cover"
            :preview-src-list="[row.imgUrl]"
            :src="row.imgUrl + '?x-oss-process=image/resize,w_100/quality,q_70'"
          />
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" :label="$t('类型')" prop="type">
        <template #default="{ row }">
          <div :style="{ color: $randomColor(row.type) }">{{ handleType(row.type) }}</div>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" :label="$t('变化数量')" prop="num" />
      <el-table-column show-overflow-tooltip align="center" :label="$t('变化后数量')" prop="afterNum" />
      <el-table-column show-overflow-tooltip align="center" :label="$t('出/入')" prop="inOut">
        <template #default="{ row }">
          <!-- 出/入 1-增加 2:减少 -->
          <div v-if="row.inOut == 1" style="color: green">{{ $t('增加') }}</div>
          <div v-if="row.inOut == 2" style="color: red">{{ $t('减少') }}</div>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" :label="$t('创建时间')" prop="createTime" />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>
<script>
import { skuShopRecordsList } from '@/api/distribution/sku/storeSkuList'
import { useDict } from '@/utils/useDict'

export default {
  data() {
    return {
      /**显示搜索条件 */
      showSearch: false,
      /**表格选中项 */
      tableCheck: [],
      sku_shop_record_type_list: [],
      /**表格数据 */
      tableData: [],
      loading: false,
      /**总条数 */
      total: 0,
      /**分页相关查询参数 */
      queryParams: {
        shopSku: '',
        pageNum: 1,
        pageSize: 20,
      },
    }
  },
  computed: {},
  mounted() {
    this.getList()
    useDict('sku_shop_record_type').then(({ sku_shop_record_type }) => {
      this.sku_shop_record_type_list = sku_shop_record_type
    })
  },
  methods: {
    handleType(type) {
      const index = this.sku_shop_record_type_list.findIndex((i) => i.dictValue == type)
      return this.sku_shop_record_type_list[index]?.dictLabel
    },

    getList() {
      this.loading = true
      skuShopRecordsList(this.queryParams)
        .then((res) => {
          this.tableData = res.rows
          this.total = res.total
        })
        .finally(() => (this.loading = false))
    },

    cancel() {
      this.$emit('update:show', false)
    },
    /** 重置按钮操作 */
    resetQuery() {
      Object.keys(this.queryParams).forEach((key) => (this.queryParams[key] = null))
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 20
      this.getList()
    },
  },
}
</script>
<style scoped lang="scss"></style>
